home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRNSAVE.PAK / OWLSCRN.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  367 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/dialog.h>
  8. #include <owl/dc.h>
  9. #include <winsys/profile.h>
  10. #include <stdio.h>
  11. #include "tscrnsav.h"
  12. #include "owlscrn.h"
  13.  
  14. //
  15. // Define USE_BWCC to use a BWCC dialog for setup
  16. //
  17. //#define USE_BWCC
  18.  
  19. #if defined(USE_BWCC)
  20. # define IDD_CONFIGURE "IDD_CONFIGURE_BWCC"
  21. #else
  22. # define IDD_CONFIGURE "IDD_CONFIGURE"
  23. #endif
  24.  
  25. //
  26. // Constants for this screen saver
  27. //
  28. const char OwlScrnSectionStr[] = "OwlScreenSaver";
  29. const char ObjectsStr[] = "Objects";
  30. const char SpeedStr[] = "Speed";
  31. char  AppName[]  = "ScreenSaver.OWLSample";
  32. const int NumThings = 900;
  33. const int MaxBuilders = 8;
  34.  
  35. //
  36. // An object on the screen
  37. //
  38. class TScreenThing {
  39.   public:
  40.     TScreenThing(TSize& scrnSize);
  41.  
  42.     virtual void Draw(TDC& dc) = 0;
  43.  
  44.   protected:
  45.     const TPoint& Position() {return Pos;}
  46.     const TColor& Color() {return C;}
  47.  
  48.   private:
  49.     TPoint  Pos;
  50.     TColor  C;
  51. };
  52.  
  53. typedef TScreenThing* (*TScreenThingBuilder)(TSize&);
  54.  
  55. //
  56. //
  57. //
  58. TScreenThing::TScreenThing(TSize& scrnSize)
  59. {
  60.   Pos.x = random(scrnSize.cx + 1);
  61.   Pos.y = random(scrnSize.cy + 1);
  62.   C = TColor(random(0x100), random(0x100), random(0x100));
  63. }
  64.  
  65.  
  66. //
  67. // A screen thing that is a small dot
  68. //
  69. class TDot : public TScreenThing {
  70.   public:
  71.     TDot(TSize& scrnSize) : TScreenThing(scrnSize) {}
  72.  
  73.     virtual void Draw(TDC& dc);
  74.  
  75.     static TScreenThing* Build(TSize& scrnSize) {return new TDot(scrnSize);}
  76. };
  77.  
  78. //
  79. //
  80. //
  81. void
  82. TDot::Draw(TDC& dc)
  83. {
  84.   dc.SetPixel(Position(), Color());
  85. }
  86.  
  87. //
  88. // A screen thing that is a line segment
  89. //
  90. class TLine : public TScreenThing  {
  91.   public:
  92.     TLine(TSize& scrnSize) : TScreenThing(scrnSize) {
  93.       End.x = random(scrnSize.cx + 1);
  94.       End.y = random(scrnSize.cy + 1);
  95.     }
  96.  
  97.     virtual void Draw(TDC& dc);
  98.  
  99.     static TScreenThing* Build(TSize& scrnSize) {return new TLine(scrnSize);}
  100.  
  101.   private:
  102.     TPoint  End;
  103. };
  104.  
  105. //
  106. //
  107. //
  108. void
  109. TLine::Draw(TDC& dc)
  110. {
  111.   dc.SelectObject(TPen(Color()));
  112.   dc.MoveTo(Position());
  113.   dc.LineTo(End);
  114.   dc.RestorePen();
  115. }
  116.  
  117. //
  118. // A larger circle screen thing
  119. //
  120. class TBlob : public TScreenThing  {
  121.   public:
  122.     TBlob(TSize& scrnSize) : TScreenThing(scrnSize) {
  123.       Rad.cx = random(scrnSize.cx/20);
  124.       Rad.cy = random(scrnSize.cy/20);
  125.     }
  126.  
  127.     virtual void Draw(TDC& dc);
  128.     static TScreenThing* Build(TSize& scrnSize) {return new TBlob(scrnSize);}
  129.  
  130.   private:
  131.     TSize  Rad;
  132. };
  133.  
  134. //
  135. //
  136. //
  137. void
  138. TBlob::Draw(TDC& dc)
  139. {
  140.   dc.SelectObject(TBrush(Color()));
  141.   dc.Ellipse(Position().x-Rad.cx, Position().y-Rad.cy,
  142.              Position().x+Rad.cx, Position().y+Rad.cy);
  143.   dc.RestoreBrush();
  144. }
  145.  
  146. //----------------------------------------------------------------------------
  147.  
  148. //
  149. //
  150. //
  151. class TMyScrnSavWindow : public TScrnSavWindow {
  152.   public:
  153.     TMyScrnSavWindow(TWindow* parent, const char* title, TModule* = 0);
  154.    ~TMyScrnSavWindow();
  155.  
  156.   protected:
  157.     char far* GetClassName() {return(AppName);}
  158.     void  GetWindowClass(WNDCLASS& wndClass);
  159.  
  160.     virtual void  AnimateScreen();
  161.  
  162.   private:
  163.     TScreenThing*  Thing[NumThings];
  164.     TScreenThingBuilder Builder[MaxBuilders];
  165.     TSize          ScreenSize;
  166.     int            DrawIndex, EraseIndex;
  167.     int            NumBuilders;
  168.     bool           DoDots;
  169.     bool           DoLines;
  170.     bool           DoBlobs;
  171. };
  172.  
  173. //
  174. //
  175. //
  176. TMyScrnSavWindow::TMyScrnSavWindow(TWindow* parent, const char* title,
  177.                                    TModule* module)
  178. :
  179.   TWindow(parent, title, module),
  180.   TScrnSavWindow(parent, title, module)
  181. {
  182.   DrawIndex  = 0;
  183.   EraseIndex = -2*(NumThings/3);
  184.   ScreenSize.cx = GetSystemMetrics(SM_CXSCREEN);
  185.   ScreenSize.cy = GetSystemMetrics(SM_CYSCREEN);
  186.  
  187.   char settings[32];
  188.  
  189.   TProfile owlScreenProfile(OwlScrnSectionStr);
  190.   owlScreenProfile.GetString(ObjectsStr, settings, sizeof settings, "1 0 0");
  191.   sscanf(settings, "%d %d %d", &DoDots, &DoLines, &DoBlobs);
  192.  
  193.   NumBuilders = 0;
  194.   if (DoDots)
  195.     Builder[NumBuilders++] = TDot::Build;
  196.   if (DoLines)
  197.     Builder[NumBuilders++] = TLine::Build;
  198.   if (DoBlobs)
  199.     Builder[NumBuilders++] = TBlob::Build;
  200.  
  201.   for (int i = 0; i < NumThings; i++)
  202.     Thing[i] = 0;
  203.  
  204.   int speed;
  205.   owlScreenProfile.GetString(SpeedStr, settings, sizeof settings, "2");
  206.   sscanf(settings, "%d", &speed);
  207.  
  208.   ((TScrnSavApp*)GetApplication())->SetSpeed(speed);
  209. }
  210.  
  211. //
  212. //
  213. //
  214. TMyScrnSavWindow::~TMyScrnSavWindow()
  215. {
  216.   for (int i = 0; i < NumThings; i++)
  217.     delete Thing[i];
  218. }
  219.  
  220. void
  221. TMyScrnSavWindow::GetWindowClass(WNDCLASS& wndClass)
  222. {
  223.   TScrnSavWindow::GetWindowClass(wndClass);
  224.   wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  225. }
  226.  
  227. //
  228. //
  229. //
  230. void
  231. TMyScrnSavWindow::AnimateScreen()
  232. {
  233.   if (!DoDots && !DoLines && !DoBlobs)
  234.     return;
  235.  
  236.   TClientDC  dc(*this);
  237.   if (dc) {
  238.     dc.SetROP2(R2_XORPEN);
  239.  
  240.     Thing[DrawIndex] = Builder[random(NumBuilders)](ScreenSize);
  241.     Thing[DrawIndex]->Draw(dc);
  242.  
  243.     DrawIndex++;
  244.     DrawIndex %= NumThings;
  245.  
  246.     if (EraseIndex >= 0) {
  247.       Thing[EraseIndex]->Draw(dc);
  248.       delete Thing[EraseIndex];
  249.       Thing[EraseIndex] = 0;
  250.       EraseIndex++;
  251.       EraseIndex %= NumThings;
  252.  
  253.     } else
  254.       EraseIndex++;
  255.   }
  256. }
  257.  
  258. //----------------------------------------------------------------------------
  259.  
  260. //
  261. //
  262. //
  263. class TScrnSavDlg : public TDialog {
  264.   public:
  265.     TScrnSavDlg(TWindow* parent, const char* name, TModule* module)
  266.       : TDialog(parent, name, module) {}
  267.  
  268.     void SetupWindow();
  269.     void CloseWindow(int retValue);
  270.  
  271.   DECLARE_RESPONSE_TABLE(TScrnSavDlg);
  272. };
  273.  
  274. DEFINE_RESPONSE_TABLE1(TScrnSavDlg, TDialog)
  275.   EV_COMMAND(IDCANCEL, CmCancel),
  276. END_RESPONSE_TABLE;
  277.  
  278. //
  279. //
  280. //
  281. void
  282. TScrnSavDlg::SetupWindow()
  283. {
  284.   TDialog::SetupWindow();
  285.   char settings[32];
  286.  
  287.   int doDots, doLines, doBlobs;
  288.   TProfile owlScreenProfile(OwlScrnSectionStr);
  289.   owlScreenProfile.GetString(ObjectsStr, settings, sizeof settings, "1 0 0");
  290.   sscanf(settings, "%d %d %d", &doDots, &doLines, &doBlobs);
  291.   CheckDlgButton(ID_DOTS, doDots);
  292.   CheckDlgButton(ID_LINES, doLines);
  293.   CheckDlgButton(ID_BLOBS, doBlobs);
  294.  
  295.   int speed;
  296.   owlScreenProfile.GetString(SpeedStr, settings, sizeof settings, "2");
  297.   sscanf(settings, "%d", &speed);
  298.   CheckRadioButton(ID_SLOW, ID_FAST, ID_SLOW+speed);
  299. }
  300.  
  301. //
  302. //
  303. //
  304. void
  305. TScrnSavDlg::CloseWindow(int retValue)
  306. {
  307.   char settings[32];
  308.  
  309.   TProfile owlScreenProfile(OwlScrnSectionStr);
  310.   sprintf(settings, "%d %d %d", IsDlgButtonChecked(ID_DOTS),
  311.                                 IsDlgButtonChecked(ID_LINES),
  312.                                 IsDlgButtonChecked(ID_BLOBS));
  313.   owlScreenProfile.WriteString(ObjectsStr, settings);
  314.  
  315.   sprintf(settings, "%d", IsDlgButtonChecked(ID_SLOW) ? 0 :
  316.                           (IsDlgButtonChecked(ID_MED) ? 1 : 2));
  317.   owlScreenProfile.WriteString(ObjectsStr, settings);
  318.  
  319.   TDialog::CloseWindow(retValue);
  320. }
  321.  
  322. //----------------------------------------------------------------------------
  323.  
  324. //
  325. //
  326. //
  327. class TMyScrnSavApp : public TScrnSavApp {
  328.   public:
  329.     TMyScrnSavApp(char far* name) : TScrnSavApp(name) {
  330. #if defined(USE_BWCC)
  331.       EnableBWCC();
  332. #endif
  333.     }
  334.  
  335.     // Override TScrnSavApp's virtual functions
  336.     //
  337.     void  InitScrnSavWindow();
  338.     void  InitConfigDialog();
  339. };
  340.  
  341. //
  342. //
  343. //
  344. void
  345. TMyScrnSavApp::InitScrnSavWindow()
  346. {
  347.   ScrnSavWnd = new TMyScrnSavWindow(0, AppName);
  348. }
  349.  
  350. //
  351. //
  352. //
  353. void
  354. TMyScrnSavApp::InitConfigDialog()
  355. {
  356.   ConfigureDialog = new TScrnSavDlg(0, IDD_CONFIGURE, this);
  357. }
  358.  
  359. //
  360. //
  361. //
  362. int
  363. OwlMain(int /*argc*/, char* /*argv*/ [])
  364. {
  365.   return TMyScrnSavApp(AppName).Run();
  366. }
  367.